<?php
/**
 * Lớp hỗ trợ đường dẫn cho view, controller
 * @author Mr.UBKey
 * @package XPHP
 * @version 0.1 B
 * @copyright XWEB
 */

class XPHP_Url
{
	/**
	 * Router của hệ thống
	 * @var XPHP_Router
	 */
	public $_router;
	
	/**
	 * Router của hệ thống
	 * @param XPHP_Router $router
	 */
	public function __construct($router=NULL)
	{
		if($router !== NULL)
			$this->_router = $router;
	}
	
	public function content($path)
	{
		if(is_file($path))
			return self::getApplicationUrl() . "/" . $path;
		else
		{
			require_once 'XPHP/Exception.php';
			throw new XPHP_Exception("Không tìm thấy tệp tin tại " . $path);
		}
	}
	
	/**
	 * Overload method action and geturl
	 */
	function __call($method_name, $arguments)
	{
    	//we inspect number of arguments
	    if($method_name == "action" && count($arguments) == 1)
	    {
	    	return $this->action1($arguments[0]);
	    }
	    else if($method_name == "action" && count($arguments) == 2 && is_string($arguments[1]))
	    {
	    	return $this->action2($arguments[0],$arguments[1]);
	    }
		else if($method_name == "action" && count($arguments) == 2 && is_array($arguments[1]))
	    {
	    	return $this->action5($arguments[0],$arguments[1]);
	    }
	    else if($method_name == "action" && count($arguments) == 3 && is_string($arguments[2]))
	    {
	    	return $this->action3($arguments[0], $arguments[1], $arguments[2]);
	    }
		else if($method_name == "action" && count($arguments) == 3 && is_array($arguments[2]))
	    {
	    	return $this->action6($arguments[0], $arguments[1], $arguments[2]);
	    }
		else if($method_name == "action" && count($arguments) == 4)
	    {
	    	return $this->action4($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
	    }
	    //application
	    else if($method_name == "getUrl" && count($arguments) == 0)
	    {
	    	return $this->getUrl1();
	    }
		else if($method_name == "getUrl" && count($arguments) == 1)
	    {
	    	return $this->getUrl2($arguments[0]);
	    }
	}
	
  	function action1($action)
  	{
    	$router = $this->_router->getInstant();
    	$router->setAction($action);
    	//Thiết lập args rỗng
    	$router->setArgs(array(),true);
    	return self::getActionUrl($router);
  	}

  	function action2($action, $controller)
  	{
    	$router = $this->_router->getInstant();
    	$router->setAction($action);
    	$router->setController($controller);
    	//Thiết lập args rỗng
    	$router->setArgs(array(),true);
    	return self::getActionUrl($router);
  	}

  	function action3($action, $controller, $area)
  	{
    	$router = $this->_router->getInstant();
    	$router->setAction($action);
    	$router->setController($controller);
    	$router->setArea($area);
    	//Thiết lập args rỗng
    	$router->setArgs(array(),true);
    	return self::getActionUrl($router);
  	}
  	
  	function action4($action, $controller, $area, $args)
  	{
    	$router = $this->_router->getInstant();
    	$router->setAction($action);
    	$router->setController($controller);
    	$router->setArea($area);
    	$router->setArgs($args, true);
    	return self::getActionUrl($router);
  	}
  	
	function action5($action, $args)
  	{
    	$router = $this->_router->getInstant();
    	$router->setAction($action);
    	$router->setArgs($args, true);
    	return self::getActionUrl($router);
  	}
  	
	function action6($action, $controller, $args)
  	{
    	$router = $this->_router->getInstant();
    	$router->setAction($action);
    	$router->setController($controller);
    	$router->setArgs($args, true);
    	return self::getActionUrl($router);
  	}
  
  	function getUrl1()
  	{
  		return self::getApplicationUrl();
  	}
  	
	function getUrl2($path)
  	{
		if(strpos($path, self::getAbsoluteUrl()) === false)
		{
			return self::getApplicationUrl(). '/' . $path;
		}
		else if(strpos($path, self::getAbsoluteUrl()) == 0)
		{
			$url = "";
			if(!empty($_SERVER['HTTPS']))
				$url .= "https://" . $_SERVER['SERVER_NAME'];
			else 
				$url .= "http://" . $_SERVER['SERVER_NAME'];
			return $url . $path;
		}	
  	}
	
  	/**
  	 * Các phương thức tĩnh của lớp URL
  	 */
	public static function getApplicationUrl()
	{
		$url = "";
		
		if(!empty($_SERVER['HTTPS']))
			$url .= "https://" . $_SERVER['SERVER_NAME'];
		else 
			$url .= "http://" . $_SERVER['SERVER_NAME'];
		
		$url .= self::getAbsoluteUrl();
		
		return $url;
	}
	
	public static function getAbsoluteUrl()
	{
		$phpself = explode('/', $_SERVER['PHP_SELF']);
		
		$url = "";
		for($i=0; $i<sizeof($phpself); $i++)
		{
			if($i != 0 && $i != sizeof($phpself) - 1)
				$url .= '/' . $phpself[$i];
		}
		
		return $url;
	}
	
	public static function getActionUrl($ro)
	{
		$url = self::getAbsoluteUrl() . '/';

		require_once 'XPHP/Router.php';
		$router = XPHP_Router::setInstance($ro);
		
		//Lấy các giá trị từ XPHP_Router
		$area = $router->getArea();
		$args = $router->getArgs();
		$controller = $router->getController();
		$action = $router->getAction();
		if(!empty($area))
		{
			$url .= $area; ;
			if(isset($args['area']))
				$url .= '-' . $args['area'];
			$url .= '/';
		}
		//Nếu controller = Index và action != index gán controller giá trị Index
		//Nếu url = default url Index/index -> $url = ''
		if(!empty($controller) && ($controller != "Index" || ($controller == "Index" && $action != "index")))	
			$url .= $controller;
		if($action != "index")
			$url .= '/' . $action;
		if(!empty($args))
		{
			foreach ($args as $key => $arg)
			{
				if((string)$key != "area")
					$url .= '/' . $arg;
			}
		}
		
		return $url;
	}
	
	public static function redirectAction($ro)             
	{
		//If param is object or is array
		if(is_object($ro) || is_array($ro))
			self::redirectUrl(self::getActionUrl($ro));
		//If is string
		else if(is_string($ro))
		{
			self::redirectUrl(self::getAbsoluteUrl().'/'.$ro);
		}
	}
	
	public static function redirectUrl($url) {
		if ($url) {
			header ( 'Location:' . $url );
			exit ();
		}
	}
	
	public static function getCurrentUrl()
	{
		$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		
		return $url;
	}
	
	public function encode($url)
	{
		$url = str_replace('/', '~', $url);
		return urlencode($url);
	}
	
	public function decode($url)
	{
		$url = urldecode($url);
		$url = str_replace('~', '/', $url);
		return $url;
	}
}